<?php

	App::import('Sanitize');
	
	class AppController extends Controller {
		var $helpers = array('Html', 'Form', 'Javascript', 'Session');
		var $components = array('Cookie', 'Session', 'RequestHandler');
		
		var $shopInfo = array();
		var $userInfo = array();
		
		var $publicAction = array();
		
		function beforeFilter() {
			parent::beforeFilter();
			// genereal settings
			$plasa = array(
				'url' => FULL_BASE_URL . $this->webroot,
				'blog' => 'http://blog.plasa.cl'
			);
			
			Configure::write('Plasa', $plasa);
			
			Security::setHash('sha1');
			
			/*
			$this->Auth->loginAction	= array('admin' => false, 'controller' => 'users', 'action' => 'login');
			$this->Auth->loginRedirect	= array('controller' => 'dashboard', 'action' => 'index');
			$this->Auth->userScope		= array('User.active' => 1, 'User.disabled' => 0, 'User.deleted' => 0);
			
			$this->Auth->accessError	= __('', true);
			$this->Auth->loginError		= __('No, you fool! That\'s not the right password!', true);
			$this->Auth->authError		= __('Sorry, you are lacking access.', true);
			
			$this->Auth->autoRedirect	= false;
			
			$this->Cookie->name			= 'PlasaAccount';
			$this->Cookie->time			= '1 hour';
			$this->Cookie->path			= $this->base;
			$this->Cookie->domain		= env('HTTP_HOST');
			$this->Cookie->secure		= true;
			$this->Cookie->key			= 'P1424M4R!<37P1423';
			*/
			
			if (!$this->Session->check('Auth')) {
				if ($this->params['controller'] != 'users' && $this->params['action'] != 'login' && !in_array($this->params['action'], $this->publicAction)) {
					$this->redirect(array('controller' => 'users', 'action' => 'login'));
				}
			}
			
			//$this->params['shop'] = 'fancity';
			
			if (isset($this->params['shop'])) {
				App::import('Model', 'Shop');
				
				$shop = new Shop;
				
				if ($shop->hasAny(array('Shop.slug' => $this->params['shop'], 'disabled' => 0))) {
					$shop->Behaviors->attach('Containable');
					/*
					$shop->bindModel(
						array(
							'belongsTo' => array(
								'Profile' => array(
									'foreignKey' => 'user_id',
								)
							)
						),
						false
					);
					*/
					$_shop = $shop->find('first',
						array(
							'cacheQueries' => false,
							'conditions' => array(
								'Shop.slug' => $this->params['shop']
							),
							'fields' => array(
								'Shop.id',
								'Shop.slug',
								'Shop.name',
								'Shop.description',
								'Shop.email',
								'Shop.url',
								'Shop.logo',
								'CONCAT(\''. Configure::read('Plasa.url') . 'media/shop/\', Shop.id, \'/\', Shop.logo) AS logo',
							),
							'contain' => array(
								'Configuration' => array(
									'option',
									'value'
								),
								/*
								'Theme' => array(
									'css',
									'body'
								)
								*/
							),
							'recursive' => 1
						)
					);
					
					$this->shopInfo = $_shop['Shop']; // Deprecated
					//$this->set('shop', $this->shopInfo); // Deprecated
					
					Configure::write('Shop', $_shop['Shop']);
					Configure::write('Shop.logo', (!empty($_shop['Shop']['logo']) ? $_shop[0]['logo'] : '')); // Add logo
					Configure::write('Shop.Configuration', $_shop['Configuration']);
					//Configure::write('Shop.Theme', $_shop['Theme']);
					
					// Load components and helpers
					$this->view = 'Theme';
					$this->theme = 'shops';
					
					App::import('Components', 'RenderShops');
					$this->helpers[] = 'Shop';
					
					// Load dashboard and authorize.
					if (preg_match('/^dashboard+/', $this->params['action'])) {
						$this->layout = 'dashboard';
						if (!in_array($this->params['shop'], Set::extract('/Shop/slug', $this->Session->read('Auth.Shop')))) {
							$this->redirect(array('shop' => $this->Session->read('Auth.Shop.0.Shop.slug'), 'controller' => 'shops', 'action' => 'dashboard_index'));
						}
					}
				}
				else {
					$this->cakeError('shopNoExists', array('shopSlug' => $this->params['shop']));
				}
			}

			if (!empty($this->params['layout'])) {
				$this->layout = $this->params['layout'];
			}
			
			if ($this->RequestHandler->isAjax()) {
				header('Content-type: application/x-javascript');
				Configure::write('debug', 0);
				$this->layout = 'ajax';
			}
		}
		
		function beforeRender() {
			parent::beforeRender();
			
			if (!empty($this->shopInfo)) {
				if (!empty($this->pageTitle)) {
					$this->pageTitle = $this->pageTitle . ' - ' . $this->shopInfo['name'];
				}
				else {
					$this->pageTitle = $this->shopInfo['name'];
				}
			}
			
			// fixed to CakePHP 1.3.6
			if (!empty($this->pageTitle)) {
				$this->set('title_for_layout', $this->pageTitle);
			}
			
			// load javascript
			$jscript = array(
				'jquery'			=> '/static/js/jquery.1.4.2',
				'qtip'				=> '/static/js/plugin/jquery.qtip.1.0.0.rc3.min',
				'fancybox'			=> '/static/js/plugin/jquery.fancybox.1.3.1.pack',
				'fileuploader'		=> '/static/js/fileuploader',
				'tinymce'			=> '/static/js/tiny_mce/tiny_mce',
			);
			
			$jscript['default'][] = $jscript['jquery'];
			$jscript['default'][] = '/static/js/commons';
			
			// load css
			$cssheet = array(
				'fancybox'			=> '/static/css/fancybox',
				'fileuploader'		=> '/static/css/fileuploader',
				'default' => array(
					'/static/css/base'
				)
			);
			
			// dashboard
			if (isset($this->params['shop']) && preg_match('/^dashboard+/', $this->params['action'])) {
				$cssheet['default'][] = '/static/css/dashboard';
				$cssheet['default-ie6'][] = '/static/css/dashboard-ie';
			}
			// shop
			else if (isset($this->params['shop'])) {
				$jscript['default'][] = '/static/js/commons-shop';
				$cssheet['default'][] = '/static/css/shop';
				$cssheet['default-ie6'][] = '/static/css/shop-ie';
			}
			// frontpage
			else {
				$cssheet['default'][] = '/static/css/mini';
				$cssheet['default-ie6'][] = '/static/css/mini-ie';
			}
			
			$description_for_layout = 'Minicatalogo.cl - Comprar nunca fue más fácil y entretenido.';
			$keywords_for_layout = 'minicatalogo, minicatalogo.cl, catalogo, tienda, vestuario, tecnología, industrias';
			
			// print views
			if (!empty($this->params['layout']) && $this->params['layout'] == 'print') {
				$this->viewPath .= DS . 'print';
			}
			
			$this->set(compact('jscript', 'cssheet', 'keywords_for_layout', 'description_for_layout'));
		}
	}

?>